/** 宏类,各个游戏的宏的基类,具体功能需要
  作用是输入游戏按键,制作按键对应的操作指令表,
  定义宏并生成一个宏名称与宏函数对象对应的Array和Map(需要子类重写_setMacro函数实现)

  By setycyas @2024-09-28
*/

class MyMacro {

  /* 公有变量
  */
  pressDuration := 25 ;按键持续时长
  macroMap := Map() ;宏名称与宏函数对象的对应Map
  macroArr := [] ;可供外部访问的宏名称[],对应macroMap,目的是固定名称顺序
  testParams := [] ;测试用参数组,外部可随时访问改变
  UI := "" ;显示信息的UI,定义为空,在UI的代码中绑定,不用的话空着也行

  /* 私有变量
  */
  _face := "" ;方向探测类的实例
  _keys := [] ;按键表
  _keysDown := [] ;按下按键的命令表,每一项为按下按键对应的字符串,记录下来避免重复生成
  _keysUp := [] ;放开按键的命令表,每一项为按下按键对应的字符串,记录下来避免重复生成

  /* 构造函数,参数faceDetector为面向方向探测类的实例,keys为游戏的按键[]
  */
  __New(faceDetector, keys){

	this._face := faceDetector
	;记录按键与按键指令
	this.refreshKeys(keys)
	;添加宏名称与宏函数对象的对应,这里是个空函数,需要在子类中实现
  this._setMacro()

  }

  /* 公有函数
  */
  ;重新传入按键组
  refreshKeys(keys){
	  local i, v
	  this._keys := keys
	  this._keysDown := []
	  this._keysUp := []
	  for i, v in keys {
	    this._keysDown.push("{" . v . " down}")
	    this._keysUp.push("{" . v . " up}")
	  }
  }

  /* 私有函数
  */
  ;* 按键,按住键,放开键的函数,参数i表示_keys的第i个按键,i从1开始
  _press(i := 1){
    send(this._keysDown[i])
	sleep(this.pressDuration)
	send(this._keysUp[i])
  }
  _down(i := 1){
    send(this._keysDown[i])
  }
  _up(i := 1){
    send(this._keysUp[i])
  }
  
  ;* 按键指定Event模式,
  _press_event(i := 1){
    sendEvent(this._keysDown[i])
	  sleep(this.pressDuration)
	  sendEvent(this._keysUp[i])
  }
  _down_event(i := 1){
    sendEvent(this._keysDown[i])
  }
  _up_event(i := 1){
    sendEvent(this._keysUp[i])
  }

  ;方向按键函数
  _upDown(){
    send("{up Down}")
  }
  _upUp(){
    send("{up Up}")
  }
  _upPress(duration){
    send("{up Down}")
    sleep(duration)
    send("{up Up}")
  }
  _downDown(){
    send("{down Down}")
  }
  _downUp(){
    send("{down Up}")
  }
  _downPress(duration){
    send("{down Down}")
    sleep(duration)
    send("{down Up}")
  }
  _leftDown(){
    send("{left Down}")
  }
  _leftUp(){
    send("{left Up}")
  }
  _leftPress(duration){
    send("{left Down}")
    sleep(duration)
    send("{left Up}")
  }
  _rightDown(){
    send("{right Down}")
  }
  _rightUp(){
    send("{right Up}")
  }
  _rightPress(duration){
    send("{right Down}")
    sleep(duration)
    send("{right Up}")
  }
  _faceDown(){
    if (this._face.face() == "left") {
      send("{left Down}")
    } else {
      send("{right Down}")
    }
  }
  _faceUp(){
    if (this._face.face() == "left") {
      send("{left Up}")
    } else {
      send("{right Up}")
    }
  }
  _facePress(duration){
    if (this._face.face() == "left") {
      this._leftPress(duration)
    } else{
      this._rightPress(duration)
    }
  }
  _backDown(){
    if (this._face.back() == "left") {
      send("{left Down}")
    } else {
      send("{right Down}")
    }
  }
  _backUp(){
    if (this._face.back() == "left") {
      send("{left Up}")
    } else {
      send("{right Up}")
    }
  }
  _backPress(duration){
    if (this._face.back() == "left") {
      this._leftPress(duration)
    } else{
      this._rightPress(duration)
    }
  }

  ;* 添加宏名称与宏函数对象的对应,这里是个空函数,需要在子类中实现
  _setMacro(){
  }

}
